
#include "misc.h"
#include <rtthread.h>

char ch_2_digit(int value)
{
    char ch = '0';
    if (value == 0)
    {
        ch = '0';
    }
    else if (value == 1)
    {
        ch = '1';
    }
    else if (value == 2)
    {
        ch = '2';
    }
    else if (value == 3)
    {
        ch = '3';
    }
    else if (value == 4)
    {
        ch = '4';
    }
    else if (value == 5)
    {
        ch = '5';
    }
    else if (value == 6)
    {
        ch = '6';
    }
    else if (value == 7)
    {
        ch = '7';
    }
    else if (value == 8)
    {
        ch = '8';
    }
    else if (value == 9)
    {
        ch = '9';
    }

    return ch;
}

/* debug print : support float double */
void printf_float(float value)
{
    int int_val = value;
    float float_part = value - int_val;
    if (float_part < 0)
        float_part *= -1;

    static char str[0x20] = {0};
    static char float_str[4] = {0};
    float_part *= 1000;

    int hundred_pos = float_part / 100;
    int dec_pos = float_part;
    dec_pos = dec_pos % 100 / 10;
    int last_pos = float_part;
    last_pos = last_pos % 10;

    float_str[0] = ch_2_digit(hundred_pos);
    float_str[1] = ch_2_digit(dec_pos);
    float_str[2] = ch_2_digit(last_pos);

    if (value < 0 && int_val == 0)
        rt_sprintf(str, "-%d.%s ", int_val, float_str);
    else
        rt_sprintf(str, "%d.%s ", int_val, float_str);

    rt_kprintf("%s", str);
}
